import axios from "axios";
import app from "../main"

export default function request(config) {

  // 打开加载插件
  app.config.globalProperties.$loading.showLoading()

  // 获取实例
  let instance = axios.create({
    baseURL: "http://localhost:3000",
    timeout: 30000,
    withCredentials: true,
  })


  instance.interceptors.request.use(
    (config) => {
      return config;
    },
    error => {
      // 关闭加载插件
      app.config.globalProperties.$loading.hideLoading()
      // 打开message插件
      app.config.globalProperties.$message.show({
        title: "网络错误，请稍后再试！",
        type: "error"
      })
      console.log(error)
    })


  instance.interceptors.response.use(
    (response) => {
      // 关闭加载插件
      app.config.globalProperties.$loading.hideLoading()
      if (response.status == 200) {
        return Promise.resolve(response.data)
      }
      app.config.globalProperties.$message.show({
        title: "网络错误，请稍后再试！",
        type: "error"
      })
    },
    error => {
      // 关闭加载插件
      app.config.globalProperties.$loading.hideLoading()
      app.config.globalProperties.$message.show({
        title: "网络错误，请稍后再试！",
        type: "error"
      })
      console.log(error)
    }
  )

  return instance(config)

}